/*
 * $Id: ApplicationServiceTestCase.java 43 2007-05-14 10:18:31Z aaron.day $
 */
package org.crazydays.core;


import org.apache.hivemind.Registry;
import org.apache.hivemind.impl.RegistryBuilder;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.HibernateException;

import junit.framework.TestCase;

import org.crazydays.core.data.PolisApplication;
import org.crazydays.hibernate.HibernateUtil;

/**
 * ApplicationServiceTestCase
 */
public class ApplicationServiceTestCase
	extends TestCase
{
	/** application service */
	private IApplicationService applicationService;

	/** session */
	private Session session;

	public void setUp()
	{
		// hivemind registry
		Registry registry = RegistryBuilder.constructDefaultRegistry();

		// application service
		applicationService = (IApplicationService) registry
				.getService(IApplicationService.class);
		assertNotNull("applicationService == null", applicationService);

		// hibernate session
		session = HibernateUtil.getSession();
		assertNotNull("session == null", session);
	}

	/**
	 * Cleanup hibernate session.
	 */
	public void tearDown()
	{
		assertNotNull("session == null", session);
		session.close();
	}

	/** application: manager user */
	public final static String APPLICATION_MANAGER_USERS = "Manage Users";

	/**
	 * Test load application.
	 */
	public void testLoadApplication()
	{
		assertNotNull("applicationService == null", applicationService);
		assertNotNull("session == null", session);

		PolisApplication application = null;
		Transaction transaction = null;

		try {
			// begin transaction
			transaction = session.beginTransaction();

			// load Manage Users
			application = applicationService.loadApplication(
					session, APPLICATION_MANAGER_USERS);
			assertNotNull("application == null", application);

			// load Invalid
			application = applicationService.loadApplication(
					session, "Invalid");
			assertNull("application != null", application);

			// commit transaction
			transaction.commit();
		} catch (HibernateException e) {
			fail("HibernateException: " + e.getMessage());
		} catch (DataException e) {
			fail("DataException: " + e.getMessage());
		}
	}
}
